<?php

namespace App\Http\Api\Controllers;

use App\Exceptions\InvalidRequestException;
use App\Http\Api\Services\OrderService;
use App\Http\Controllers\Controller;
use App\Http\Models\Order;
use App\Lib\PHPExcel\ExcelHandle;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class OrderController extends Controller
{
    protected $orderService;

    public function __construct(OrderService $orderService)
    {
        $this->orderService = $orderService;
    }

    public function codeEdit(Request $request){
        $attributes = $request->input('data') ?? null;

        $result = $this->orderService->codeEdit($attributes);

        return res('更新完成', $result);
    }

    /*
     * 订单采集 from管家婆
     * */
    public function store(Request $request)
    {
        $attributes = $request->input('data') ?? null;

        $result = $this->orderService->store($attributes);

        return res('采集完成', $result);
    }
   /*
    * 商家结算订单采集 from放心购后台插件
    * */
    public function settStore(Request $request)
    {
        $attributes = $request->input('data') ?? null;

        $result = $this->orderService->settStore($attributes);

        return res('采集完成', $result);
    }

    /*
     * 曲线-主播订单统计
     */
    public function orderCount(Request $request){
        $attributes = $request->all();

        $result = $this->orderService->orderCount($attributes);

        return res('ok', $result);
    }

    /*
     * 退款率报表接口
     * */
    public function refundReport(Request $request){
        $attributes = $request->all();

        $result = $this->orderService->refundReport($attributes);

        return res('ok', $result);
    }

    /*
     * 用户退货率
     * */
    public function userRate(){
        $raw = DB::raw("total_amount,post_receiver,post_tel,shop_name,order_status,create_time,province,city,detail,shop_name");
        $query = Order::select($raw)->where('create_time','>','2019-07-01 00:00:00')->where('liver','沈立妙')->whereNotIn('order_status',[1,4]);
        $tmp_arr = [];

        $orders = $query->get()->toArray();

        foreach ($orders as $order){
            if(!isset($tmp_arr[$order['post_tel']])){

                $complete_order = $order['order_status'] == 5 || $order['order_status'] == 3 ? 1: 0;
                if($order['order_status'] == 6 || $order['order_status'] == 27){
                    $thdd = 1;
                }else{
                    $thdd = 0;
                }

                if($order['order_status'] == 21 || $order['order_status'] == 22 || $order['order_status'] == 16 || $order['order_status'] == 39){
                    $tkdd = 1;
                }else{
                    $tkdd = 0;
                }

                if($order['order_status'] == 2 ){
                    $bhdd = 1;
                }else{
                    $bhdd = 0;
                }



                $tmp_arr[$order['post_tel']] = [
                    'post_tel' => $order['post_tel'],
                    'post_receiver' => $order['post_receiver'],
                    'order_time_1' => $order['create_time'],
                    'order_time_2' => $order['create_time'],
                    'total_amount' => $complete_order ? $order['total_amount'] : 0,
                    'order_num' => 1,
                    'complete_order' => $complete_order,
                    'thdd' => $thdd,
                    'tkdd' => $tkdd,
                    'bhdd' => $bhdd,
                    'detail' => $order['province'].$order['city'].$order['detail'],
                    'shops' => [
                        $order['shop_name']
                    ]
                ];


            }else{
                $complete_order_flag = $order['order_status'] == 5 || $order['order_status'] == 3 ? 1: 0;

                if($order['order_status'] == 6 || $order['order_status'] == 27){
                    $thdd_flag = 1;
                }else{
                    $thdd_flag = 0;
                }

                if($order['order_status'] == 21 || $order['order_status'] == 22 || $order['order_status'] == 16 || $order['order_status'] == 39){
                    $tkdd_flag = 1;
                }else{
                    $tkdd_flag = 0;
                }

                if($order['order_status'] == 2 ){
                    $bhdd_flag = 1;
                }else{
                    $bhdd_flag = 0;
                }
                if(!in_array($order['shop_name'],$tmp_arr[$order['post_tel']]['shops'])){
                    $shops = array_merge($tmp_arr[$order['post_tel']]['shops'],[$order['shop_name']]);
                }else{
                    $shops = $tmp_arr[$order['post_tel']]['shops'];
                }


                $tmp_arr[$order['post_tel']] = [
                    'post_tel' => $order['post_tel'],
                    'post_receiver' => $order['post_receiver'],
                    'order_time_1' => $tmp_arr[$order['post_tel']]['order_time_1'],
                    'order_time_2' => $order['create_time'],
                    'total_amount' => $complete_order_flag ? $order['total_amount'] + $tmp_arr[$order['post_tel']]['total_amount']: $tmp_arr[$order['post_tel']]['total_amount'],
                    'order_num' => $tmp_arr[$order['post_tel']]['order_num'] + 1,
                    'complete_order' => $complete_order_flag ? $tmp_arr[$order['post_tel']]['complete_order'] + 1:$tmp_arr[$order['post_tel']]['complete_order'],
                    'thdd' => $thdd_flag ? $tmp_arr[$order['post_tel']]['thdd'] + 1:$tmp_arr[$order['post_tel']]['thdd'],
                    'tkdd' => $tkdd_flag ? $tmp_arr[$order['post_tel']]['tkdd'] + 1:$tmp_arr[$order['post_tel']]['tkdd'],
                    'bhdd' => $bhdd_flag ? $tmp_arr[$order['post_tel']]['bhdd'] + 1:$tmp_arr[$order['post_tel']]['bhdd'],
                    'detail' => $order['province'].$order['city'].$order['detail'],
                    'shops' => $shops
                ];




            }
        }

        $phones = collect($tmp_arr)->pluck('post_tel');



        $p = DB::table('fans_phones')->select('fans.nickname','fans_phones.phone')->LeftJoin('fans','fans.unique_id','fans_phones.fans_id')->whereIn('fans_phones.phone',$phones)->get()->toArray();
		



        //再次处理
        foreach ($tmp_arr as &$item){
            $item['wc_rate'] = round($item['complete_order'] / $item['order_num'] * 100, 2);
            $item['wc_rate'] = $item['wc_rate'] . '%';

            $item['th_rate'] = round($item['thdd'] / $item['order_num'] * 100, 2);
            $item['th_rate'] = $item['th_rate'].'%';

            $item['tk_rate'] = round($item['tkdd'] / $item['order_num'] * 100, 2);
            $item['tk_rate'] = $item['tk_rate'].'%';

            $item['shops'] = implode(',',$item['shops']);
            $nick = collect($p)->where('phone', $item['post_tel'])->first();

            $item['nickname'] = $nick !=null ? $nick->nickname: '';
        }



        $title = '订单 用户退款率' ;

        ExcelHandle::factory('TkRate', $title, $tmp_arr)->export();
    }



}
